package br.com.center.loja.mb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.center.loja.data.RepositoryFactoryJPA;
import br.com.center.loja.model.City;


@FacesConverter(forClass=City.class, value="cityConverter")
public class CityConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext context, 
			                  UIComponent component, 
			                  String value) {
		try {
			return RepositoryFactoryJPA
					.create(City.class)
					.select(Long.valueOf(value));
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext context, 
			                  UIComponent component, 
			                  Object value) {
		try {
			return ((City)value).getCode().toString();			
		} catch (Exception e) {
			return null;
		}
	}

}
